<?php
 
namespace console\controllers;  
use Weixin\Dialog\DataCube;
use backend\models\weixin\WeiFansCount; 
/**
 * 微信数据统计
 */
class WeixinController extends \yii\console\Controller  
{  
    /**
     * 定时获取用户增减数据
     */
    public function actionUser()
    {
        $yesterday = $this->formatTime();

        //获取用户增减数据
        $summary = DataCube::getDataCube('getusersummary', $yesterday, $yesterday);
        if (! $summary) {
            //记录日志，请求错误
            $err = DataCube::getError();
            \Yii::error('weixin-getusersummary:' . $err['errcode']);
            return 1;
        }

        //获取用户总量
        $cumulate = DataCube::getDataCube('getusercumulate', $yesterday, $yesterday);
        if (! $cumulate) {
            //记录日志，请求错误
            $err = DataCube::getError();
            \Yii::error('weixin-getusercumulate:' . $err['errcode']);
            return 1;
        }

        //入库
        $weiFansCount = new WeiFansCount;
        $weiFansCount->saveFromWei($summary, $cumulate);
        return 0;

    }

    /**
     * 返回前一天时间格式 2016-01-04
     */
    private function formatTime()
    {
        return date('Y-m-d', strtotime("-1 day"));
    }

} 